package com.houtai.service.article;

import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzArticleInfo;
import com.houtai.bean.dto.article.CampusArticleListDto;
import com.houtai.dao.TsJzArticleInfoDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/19.
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    private static Logger logger = Logger.getLogger(ArticleServiceImpl.class);
    @Resource
    private TsJzArticleInfoDao tsJzArticleInfoDao;


    @Override
    public RetInfo findCampusArticlePage(CampusArticleListDto dto) {
        String logInfo =  ":findCampusArticlePage:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int page_size = dto.getPage_size();
            int current_page = dto.getCurrent_page();
            Map<String, Object> result = new HashMap<>();
            int total_record = tsJzArticleInfoDao.findCampusArticleSize(dto);
            int total_page;
            if (total_record % page_size == 0) {
                total_page = total_record / page_size;
            } else {
                total_page = (total_record / page_size) + 1;
            }
            if (current_page <= 1) {
                current_page = 1;
            }
            dto.setCurrent_page((current_page - 1) * page_size);

            List<TsJzArticleInfo> tsJzArticleInfos =
                    tsJzArticleInfoDao.findCampusArticleList(dto);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            result.put("article", tsJzArticleInfos);
            result.put("current_page", current_page);
            result.put("page_size", page_size);
            result.put("total_record", total_record);
            result.put("total_page", total_page);
            result.put("arti_type", dto.getArti_type());
            result.put("arti_name", dto.getArti_name());
            result.put("campus_id", dto.getCampus_id());

            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }


    @Override
    public RetInfo insertArticle(TsJzArticleInfo tsJzArticleInfo) {
        String logInfo =  ":insertArticle:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tsJzArticleInfoDao.insertArticle(tsJzArticleInfo);

            retInfo.setMark("0");
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo updateArticle(TsJzArticleInfo tsJzArticleInfo) {
        String logInfo =  ":updateArticle:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tsJzArticleInfoDao.updateArticle(tsJzArticleInfo);
            retInfo.setMark("0");
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo updateArticleValid(TsJzArticleInfo tsJzArticleInfo) {
        String logInfo =  ":updateArticleValid:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tsJzArticleInfoDao.updateArticleValid(tsJzArticleInfo);
            retInfo.setMark("0");
            retInfo.setTip("成功");

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TsJzArticleInfo findArticleById(Integer id) {
        return tsJzArticleInfoDao.findArticleById(id);
    }
}
